<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css" />
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th ></th>
							<th class="text-center" data-field="regionName">区域</th>
							<th class="text-center" data-field="number">编号</th>
							<th class="text-center" data-field="tel">手机号</th>
							<th class="text-center" data-field="licensePlate">车牌号</th>
							<th class="text-center" data-field="endTime">过期时间</th>
							<th class="text-center" data-field="state">状态</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/js/date.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>

	function actionFormatter(value, row, index) {
		//console.log(row);
		return [ "<button class='btn btn-info btn-xs glyphicon glyphicon-file' onclick='updateParkingSpace(\"" + row.id + "\")'>编辑</button>",
					"&nbsp;<button class='btn btn-danger btn-xs glyphicon glyphicon-trash' onclick='delParkingSpace(\"" + row.id + "\")'>删除</button>",].join('');
	}
	
	var table_dataset = [ {
        checkbox : true
    }, {
		field : 'regionName',
		align : 'center',
	}, {
		field : 'number',
		align : 'center',
		formatter : function Formatter(value, row, index) {
			if(row.number < 10){
				return "00"+row.number;
			}else if(row.number < 99){
				return "0"+row.number;
			}else{
				return row.number;
			}
		}
	},{
		field : 'tel',
		align : 'center',
		formatter : function Formatter(value, row, index) {
			if(row.tel==null||row.tel==''){
				return "无";
			}else{
				return row.tel;
			}
		}
	},{
		field : 'licensePlate',
		align : 'center',
		formatter : function Formatter(value, row, index) {
			if(row.licensePlate==null||row.licensePlate==''){
				return "无";
			}else{
				return row.licensePlate;
			}
		}
	}, {
		field : 'endTime',
		align : 'center',
		formatter : function Formatter(value, row, index) {
			if(row.endTime==null||row.endTime==''){
				return "无";
			}else{
				return row.endTime;
			}
		}
	},{
		field : 'state',
		align : 'center',
		width : 200,
		formatter : function Formatter(value, row, index) {
			if(row.state=='0'){
				return "<div class='btn-group'><button class='btn btn-default btn-xs dropdown-toggle' type='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>"
			    +"出租 <span class='caret'></span></button><ul class='dropdown-menu'><li><a onclick='updateState(\"" + row.id+ "\",1)'>空置</a></li></ul></div>";
			}else if(row.state=='1'){
				return "<div class='btn-group'><button class='btn btn-success btn-xs dropdown-toggle' type='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>"
			    +"空置 <span class='caret'></span></button><ul class='dropdown-menu'><li><a onclick='updateState(\"" + row.id+ "\",0)'>出租</a></li><li><a onclick='updateState(\"" + row.id+ "\",2)'>出售</a></li></ul></div>";
			}else{
				return "<div class='btn-group'><button class='btn btn-primary btn-xs dropdown-toggle' type='button' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>"
			    +"出售 <span class='caret'></span></button><ul class='dropdown-menu'><li><a onclick='updateState(\"" + row.id+ "\",1)'>空置</a></li></ul></div>";
			}
		}
	}, {
		align : 'center',
		width : 300,
		formatter : actionFormatter
	}];
	$('#table').bootstrapTable(
			{
				url : "${pageContext.request.contextPath}/selectParkingSpacePage",
				idField : "sid",
				columns : table_dataset,
				striped : true, //是否显示行间隔色
				pagination : true, //是否显示分页
				pageSize : 10, //每页的记录行数
				pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
				search : false, //是否显示搜索
				showRefresh : true,
				clickToSelect : true, //是否启用点击选中行
				sidePagination : "server", //表示服务端请求  
				queryParamsType : "limit",
				queryParams : function queryParams(params) { //设置查询参数  
					var param = {
						offset : params.offset,
						limit : params.limit,
						name : $("#name1").val()
						
					};
					return param;
				},
				toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='addParkingSpace(\"" + '' + "\");'>批量添加</button>"
						  +"&nbsp;<button class='btn btn-primary glyphicon glyphicon-plus' onclick='updateParkingSpace(\"" +'' + "\")'>普通录入</button>"
						  +"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash' onclick='dels()'>批量删除</button>"
						  + "<div class='input-group' style='width:250px;float:right;margin-top:2px;margin-left:20px;'><input type='text' class='form-control' placeholder='请输入编号' id='name1' name='name1'>"
						  + "<span class='input-group-btn'><button class='btn btn-default' type='button' onclick='query(\"" + '' + "\");'>查询</button></span></div>",
				icons : {
					paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
					paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
					refresh : 'glyphicon-refresh icon-refresh',
					toggle : 'glyphicon-list-alt icon-list-alt',
					columns : 'glyphicon-th icon-th',
					detailOpen : 'glyphicon-plus icon-plus',
					detailClose : 'glyphicon-minus icon-minus',
					"export" : 'glyphicon-export icon-share'
				},
				showExport : true,
				exportDataType : 'basic',
				exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ],
			});
	
	//弹窗
	function addParkingSpace(id) {
		layer.open({
			type: 2,
			area: ['500px', '485px'],
			fix: false, //不固定
			maxmin: true,
			shade:0.4,
			offset: [50],
			title: '批量添加房屋',
			content: '${pageContext.request.contextPath}/jump_addParkingSpaceList',
			btn : [ "保存" ],
			btn1 : function(index, layero) {
				var iframeWin = window[layero.find('iframe')[0]['name']];
				iframeWin.insert();//执行子页面的方法
			}
		});

	}
	
	function updateParkingSpace(id) {
		layer.open({
			type: 2,
			area: ['500px', '450px'],
			fix: false, //不固定
			maxmin: true,
			shade:0.4,
			offset: [50],
			title: '编辑',
			content: '${pageContext.request.contextPath}/jump_editParkingSpace?id=' + id,
			btn : [ "保存" ],
			btn1 : function(index, layero) {
				var iframeWin = window[layero.find('iframe')[0]['name']];
				iframeWin.insert();//执行子页面的方法
			}
		});

	}
	
	
	function updateState(id,state){
		if(state==1){
			$.ajax({
				url : "${pageContext.request.contextPath}/updateParkingSpace",
				type : "post",
				data : {
					id : id,
					state:state,
					tel:'',
					licensePlate:'',
					endTime:''
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		}else{
			layer.open({
				type: 2,
				area: ['400px', '290px'],
				fix: false, //不固定
				maxmin: true,
				shade:0.4,
				offset: [50],
				title: '绑定用户',
				content: '${pageContext.request.contextPath}/jump_lookPeople?id=' + id+'&state='+state,
				btn : [ "保存" ],
				btn1 : function(index, layero) {
					var iframeWin = window[layero.find('iframe')[0]['name']];
					iframeWin.insert();//执行子页面的方法
				}
			});
		}
	}
	

	function delParkingSpace(id) {
		layer.confirm('确认要删除吗？', function (index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/delParkingSpace",
				type : "post",
				data : {
					id : id
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}
	
    //批量删除
    function dels() {
        var ids = [];
        $.each($('#table').bootstrapTable('getSelections'), function (k, v) {
            ids.push(v.id);
        });
        if (ids.length > 0) {
        	layer.confirm('确认要删除吗？', function (index) {
                $.ajax({
                    url: '${pageContext.request.contextPath}/delParkingSpaceByIds',
                    type: 'post',
                    data: {
                        ids: ids
                    },
                    success: function (data) {
                        if (data.result === 1) {
                            layer.msg('删除成功!', {icon: 1, time: 800});
                            $('#table').bootstrapTable('refresh');
                        } else {
                            layer.msg('删除失败!', {icon: 5, time: 800});
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        layer.msg('系统异常!', {icon: 2, time: 800});
                    }
                });
            });
        } else {
            layer.alert('请先选择要删除的项！')
        }
    }

	function querry() {
		$("#table").bootstrapTable('refresh');
		$("#name1").val('');
	}
</script>
</html>
